//
//  CLBaseViewController.m
//  InterestingVideos
//
//  Created by CoderLey on 2022/8/17.
//

#import "CLBaseViewController.h"

@interface CLBaseViewController ()
@property UIImageView *navBarHairlineImageView;
@end

@implementation CLBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    if (self.navigationController.viewControllers.count > 1) {
        [self backButtonItemImageNamed:@"fanhui1" target:self action:@selector(backButtonItemClicked:)];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navBarHairlineImageView.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navBarHairlineImageView.hidden = YES;
}
- (void)presentToNewViewController:(CLBaseViewController *)newViewController hiddenView:(UIView *)hiddenView;{
    KWeakSelf;
    newViewController.viewControlCloseBlock = ^(BOOL isRoot) {
        if (hiddenView != nil) {
            hiddenView.hidden = NO;
        }
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    };
    [self presentViewController:newViewController animated:YES completion:^{
        if (hiddenView != nil) {
            hiddenView.hidden = YES;
        }
    }];
}
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)doubleTabBarAction{};

- (void)backButtonItemImageNamed:(NSString*)imgName target:(id)target action:(SEL)selector {
    UIImage* backImg = [[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithImage:backImg style:UIBarButtonItemStylePlain target:target action:selector];
    self.navigationItem.leftBarButtonItem = item;
    //启用返回手势，需要解决卡死问题
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}

- (void)backButtonItemClicked:(UIButton *)sender {
//    if([self isKindOfClass:[IVMeRefundProgressViewController class]]){
//        for (UIViewController *vc in self.navigationController.viewControllers) {
//            if ([vc isKindOfClass:[IVMeServiceCentreViewController class]]) {
//                IVMeServiceCentreViewController *detailVC = (IVMeServiceCentreViewController *)vc;
//                [self.navigationController popToViewController:detailVC animated:YES];
//                break;
//            }
//        }
//    }else{
        [self.navigationController popViewControllerAnimated:YES];
//    }
}

- (void)setBackgroundImage:(NSString *)imageName {
    UIImageView *background = [[UIImageView alloc] initWithFrame:self.view.bounds];
    background.clipsToBounds = YES;
    background.contentMode = UIViewContentModeScaleToFill;
    background.image = [UIImage imageNamed:imageName];
    [self.view addSubview:background];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
